package com.sskj.givecoin.p.fragment;

import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.common.MarketWebSocket;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.bean.LevelBean;
import com.sskj.givecoin.m.bean.StockBean;
import com.sskj.givecoin.m.bean.UserInfo;
import com.sskj.givecoin.m.bean.bean.LevelTransactionCodePk;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.LevelTransactionFragmentX;

import java.util.List;

/**
 * Created by Administrator on 2018/4/9 0009.
 */

public class LevelTransactionFragmentPst extends BasePresenter<LevelTransactionFragmentX> {
    private MarketWebSocket btcWebSocket;

//    public void requestOrderList(String code) {
//        if (!TextUtils.isEmpty(MyAppLication.getMobile())) {
//            OkGo.<HttpData<List<TradeDealBean>>>post(HttpConfig.BASE_URL + HttpConfig.TRADE_CHENGJIAO_ALL)
//                    .params("mobile", MyAppLication.getMobile())
//                    .params("code", code)
//                    .execute(new JsonCallBack<HttpData<List<TradeDealBean>>>(this) {
//                        @Override
//                        public void _onSuccess(Response<HttpData<List<TradeDealBean>>> response) {
//                            HttpData<List<TradeDealBean>> body = response.body();
//                            if (body.getStatus() == 200) {
//                                mView.onOrderListResponseSuccess(body.getData());
//                            } else {
//                                mView.onOrderListResponseSuccess(null);
//                            }
//                        }
//
//                        @Override
//                        public void onError(Response<HttpData<List<TradeDealBean>>> response) {
//                            if (response.getException() instanceof JsonParseException) {
//                                mView.onOrderListResponseSuccess(null);
//                            } else {
//                                super.onError(response);
//                            }
//
//                        }
//                    });
//        }
//    }

    public void getUserInfo() {
        OkGo.<HttpData<UserInfo>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<UserInfo>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            mView.updateWallet(httpData.getData().wallone);
                        }
                    }
                });
    }

    public void createOrder(StockBean stockData, boolean isTime, boolean isWeituo,String leverage) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.TRADE_CREATE)
                .params("mobile", MyAppLication.getMobile())
                .params("newprice", stockData.newprice)
                .params("type", stockData.type)
                .params("buynum", stockData.buynum)
                .params("shopname", isTime ? stockData.shopname : stockData.shopname.toLowerCase())
                .params("otype", stockData.otype)
                .params("zy", stockData.stopwin)
                .params("zs", stockData.stoploss)
                .params("leverage", leverage)
//                .params("pwd", pwd)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData HttpData = response.body();
                        if (HttpData.status == 200) {
                            mView.onCreateOrderSuccess(isWeituo);
                        } else {
                            ToastUtil.showShort(HttpData.msg);
                        }
                    }
                });
    }

    public void getLeveRange(String code) {
        Log.i("Info123", "code::::" + code);
        OkGo.<HttpData<LevelBean>>post(HttpConfig.BASE_URL + HttpConfig.LEVEL)
                .params("code", code)
                .execute(new JsonCallBack<HttpData<LevelBean>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<LevelBean>> response) {
                        mView.upDateLevel(response.body().getData());
                    }
                });
    }
    /**
     * 获取盘口数据
     * @param code
     */
    public void getPanKouData(String code) {
        OkGo.<HttpData<List<LevelTransactionCodePk>>>get(HttpConfig.BASE_URL + HttpConfig.GETDEPTH)
                .params("code", code)
                .execute(new JsonCallBack<HttpData<List<LevelTransactionCodePk>>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<List<LevelTransactionCodePk>>> response) {
                        if(response.body().getData().size()>0){
                            mView.getPanKouDataSuccess(response.body().getData().get(0));
                        }
                    }
                });
    }
    public void initWebSocket(String type) {
//        String text = "{\"code\":\"all\"}";
        String text = "{\"code\":\"" + type + "\"}";
        if (btcWebSocket == null) {
            mView.flag = true;
            btcWebSocket = new MarketWebSocket(HttpConfig.BTC_PK, "marketList", text);
            btcWebSocket.setListener(message -> mView.update(message));
            mView.sellList.clear();
            mView.buyList.clear();
        }
    }

    public void closeWebSocket() {
//        if (btcWebSocket != null) {
//            btcWebSocket.closeWebSocket();
//            mView.flag = false;
//            btcWebSocket = null;
//        }
    }


}
